﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;

namespace InsertEmoticonPlugin
{
    public class ScottIsAFool
    {
        Regex r;
        Regex r2;
        XDocument doc;
        Settings m_settings;

        public ScottIsAFool(Settings settings)
        {
            m_settings = settings;
        }

        public ScottIsAFool() { }

        public string Emotify(string source)
        {
            //string xmlfile = @"c:\program files\windows live\writer\plugins\Emoticons.xml";
            string xmlfile = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\Windows Live\\Writer\\Plugins\\Emoticons.xml";
            doc = XDocument.Load(xmlfile);
            //string emotregex = @"(\((?:[a-i]|[k-p]|r|S|t|u|[w-z]|so|brb|ci|\*|@|&amp;|sn|[bn]ah|tu|\{|\}|0|6|8|~|mp|ap|au|mo|co|xx|h5|yn|pi|pl|\^|\|\||%|ip|#|st|li|um|\?)\)|:\-?[(&lt;|&gt;)\(\)\$\|\[]|;\-?\)|:\'\(|:\-?[dops]|:\-?@|8o\||\^o\)|\*\-\)|8\-\)|:\^\)|8\-\||\+o\(|&lt;:o\)|\|\-\)|:\-#|:\-\*)";
            string emotregex = @"(\((?:[a-i]|[k-p]|r|S|t|u|[w-z]|so|brb|ci|\*|@|&amp;|sn|[bn]ah|tu|\{|\}|0|6|8|~|mp|ap|au|mo|co|xx|h5|yn|pi|pl|\^|\|\||%|ip|#|st|li|um|\?)\)|:\-?(?:&lt;|&gt;|[\(\)\$\|\[])|;\-?\)|:\'\(|:\-?[dops]|:\-?@|8o\||\^o\)|\*\-\)|8\-\)|:\^\)|8\-\||\+o\(|&lt;:o\)|\|\-\)|:\-#|:\-\*)";
            r2 = new Regex(@"(^|>)(.*?)(<|$)", RegexOptions.Singleline);
            r = new Regex(emotregex, RegexOptions.Singleline | RegexOptions.IgnoreCase);
            string newsource = r2.Replace(source, TextReplacer);

            return newsource;
        }

        public int EmoticonCount(string source)
        {
            string emotregex = @"(\((?:[a-i]|[k-p]|r|S|t|u|[w-z]|so|brb|ci|\*|@|&|sn|[bn]ah|tu|\{|\}|0|6|8|~|mp|ap|au|mo|co|xx|h5|yn|pi|pl|\^|\|\||%|ip|#|st|li|um|\?)\)|:\-?[<>\(\)\$\|\[]|;\-?\)|:\'\(|:\-?[dops]|:\-?@|8o\||\^o\)|\*\-\)|8\-\)|:\^\)|8\-\||\+o\(|<:o\)|\|\-\)|:\-#|:\-\*)";

            Regex r = new Regex(emotregex, RegexOptions.IgnoreCase);
            MatchCollection mcl = r.Matches(source);
            return mcl.Count;
        }

        private string TextReplacer(Match match)
        {
            string prefix = match.Groups[1].Value;
            string postfix = match.Groups[3].Value;

            string emoticons = r.Replace(match.Groups[2].Value, EmoticonReplacer);

            return prefix + emoticons + postfix;
        }

        private string EmoticonReplacer(Match match)
        {
            // decode for emoticons like (&)
            string emoticoncode = match.Groups[1].Value;
            if (!emoticoncode.Equals("(s)"))
                emoticoncode = emoticoncode.ToLower();
            else
                return emoticoncode;
            try
            {
                var emot = (from emoticon in doc.Descendants("emoticon")
                            where emoticon.Element("shortcode").Value.Contains(HttpUtility.HtmlDecode(emoticoncode))
                            select new
                            {
                                filename = emoticon.Element("filename").Value,
                                AltText = emoticon.Element("alttext").Value
                            }).Take(1).Single();
                string filename = emot.filename;
                string alttext = emot.AltText;
                if (m_settings != null)
                {
                    StringBuilder img = new StringBuilder();
                    img.Append(string.Format("<img alt=\"{0}\" src=\"http://spaces.live.com/rte/emoticons/{1}.gif\"", alttext, filename));
                    if (m_settings.NonDefaultSize)
                    {
                        img.Append(string.Format(" height=\"{0}\" width=\"{0}\">", m_settings.Size.ToString()));
                    }
                    else
                    {
                        img.Append(">");
                    }

                    return img.ToString();
                }
                else
                {
                    string img = string.Format("<img alt=\"{0}\" src=\"http://spaces.live.com/rte/emoticons/{1}.gif\">", alttext, filename);
                    return img;
                }
            }
            catch
            {
                return emoticoncode;
            }
        }

    }
}
